package com.shiwaixiangcun.customer.module.watch;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.utils.permission.DefaultRationale;
import com.shiwaixiangcun.customer.utils.permission.PermissionSetting;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.Rationale;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.QRCodeView;

/**
 * 绑定
 *
 * @author Administrator
 */
public class BindWatchActivity extends BaseActivity implements View.OnClickListener, QRCodeView.Delegate {

    /**
     * IMEI 长度
     */
    private static final int IMEI_LENGTH = 15;
    @BindView(R.id.qrcode_view)
    QRCodeView mQRCodeView;
    @BindView(R.id.back_left)
    ChangeLightImageView mBackLeft;
    @BindView(R.id.tv_page_name)
    TextView mTvPageName;
    @BindView(R.id.tv_inputimei)
    TextView mTvInputimei;
    private PermissionSetting mSetting;
    private Rationale mRationale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_watch);
        ButterKnife.bind(this);


        initViewAndEvent();
        requestPermission(Permission.Group.CAMERA);

    }

    /**
     * 获取相机权限
     *
     * @param permissions
     */
    private void requestPermission(String[] permissions) {
        AndPermission.with(this)
                .permission(permissions)
                .rationale(mRationale)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        initData();
                    }
                })
                .onDenied(new Action() {
                    @Override
                    public void onAction(@NonNull List<String> permissions) {
                        showToastShort("未获取到相关权限");
                        if (AndPermission.hasAlwaysDeniedPermission(BindWatchActivity.this, permissions)) {
                            mSetting.showSetting(permissions);
                        }
                    }
                })
                .start();
    }

    private void initData() {
        mQRCodeView.startCamera();
        mQRCodeView.startSpotAndShowRect();
        mQRCodeView.changeToScanBarcodeStyle();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    /**
     * 初始化试图
     */
    private void initViewAndEvent() {
        mTvPageName.setText(R.string.binding_watch);

        mQRCodeView.setDelegate(this);
        mBackLeft.setOnClickListener(this);
        mTvInputimei.setOnClickListener(this);
        mRationale = new DefaultRationale();
        mSetting = new PermissionSetting(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_left:
                finish();

                break;
            case R.id.tv_inputimei:
                readyGoThenKill(BindWatchSelfActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
        mQRCodeView.showScanRect();
        mQRCodeView.startSpotAndShowRect();
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }


    /**
     * 显示对话框
     *
     * @param content 对话框提示内容
     */

    public void showDialog(String content) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(content);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                mQRCodeView.startCamera();
                mQRCodeView.showScanRect();
                mQRCodeView.startSpot();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();

        if (result == null) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("imei", result);
//        bundle.putString("imei", "868219000645207");
        Log.e(BUG_TAG, result);
        if (result.length() != IMEI_LENGTH) {
            showDialog("无效条形码，请扫描智能手表设备包装盒上的IMEI条形码!");
            vibrate();
            mQRCodeView.stopSpot();
        } else {

            vibrate();
            mQRCodeView.stopSpot();
            readyGoThenKill(ConfirmBindActivity.class, bundle);

        }


    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }
}
